"""
文件名:OrderPage.py
编写人:陈急
作用:定位订单页面表现层,并操作表现层可操作元素
Page类继承Base类
"""
from Common.Base import Base, open_browser

order_url = "http://localhost:8080/ecshop/user.php"


class OrderPage(Base):
    """创建一个订单页面类"""
    """封装页面表现层"""
    loc_search_button = ("name", "imageField")  # 搜索按钮
    goods_list = ('css selector', '.goodsItem>a>img')  # 定位页面中所有的商品
    buy_goods_loc = ("css selector", "#ECS_FORMBUY > ul > li.padd > table > tbody > tr > td.td1 > a > img")  # 立即购买按钮
    shopping_cart_accounts_loc = ("css selector", "img[alt='checkout']")  # 购物车结算按钮
    shopping_goods_alter_loc = ("css selector", "#theForm > div:nth-child(2) > h6 > a")  # 订单页面商品列表修改按钮
    goods_list_sum_price_loc = ("css selector", 'td[colspan="7"]')  # 商品列表价格合计
    shopping_receiving_information_loc = ("css selector", "#theForm > div:nth-child(4) > h6 > a")  # 订单页面收货信息修改按钮
    shipping_sto_loc = ("css selector", "input[name=shipping][value='5']")  # 配送方式申通单选框
    shipping_post_office_loc = ("css selector", "input[name=shipping][value='6']")  # 配送方式邮局平邮单选框
    shipping_price_sto_loc = ("css selector", "#shippingTable > tbody > tr:nth-child(2) > td:nth-child(4)")  # 配送费-申通
    shipping_post_office_price_loc = (
        'css selector', "#shippingTable > tbody > tr:nth-child(3) > td:nth-child(4)")  # 配送费-邮政
    shipping_insured_price_loc = ("id", "ECS_NEEDINSURE")  # 配送是否报价单选框
    payment_balance_loc = ("css selector", "input[name=payment][value='1']")  # 支付方式-余额支付单选框
    payment_bank_transfer_loc = ("css selector", "input[name=payment][value='2']")  # 支付方式-银行转账
    payment_cash_on_delivery_loc = ("css selector", "input[name=payment][value='3']")  # 支付方式-货到付款
    packing_unwanted_packing_loc = ("css selector", "input[name=pack][value='0']")  # 包装方式-不需要包装
    packing_delicate_packing_loc = ("css selector", "input[name=pack][value='1']")  # 包装方式_精品包装
    packing_delicate_packing_see_loc = (
        "css selector", "#packTable>tbody > tr:nth-child(3) > td:nth-child(5) > a]")  # 精品包装-查看按钮
    card_unwanted_card_loc = ("css selector", "input[name=card][value='0']")  # 祝福贺卡-不要贺卡
    card_blessing_card_loc = ("css selector", "input[name=card][value='1']")  # 祝福贺卡-祝福贺卡
    card_blessing_card_see_loc = (
        "css selector", "#cardTable > tbody > tr:nth-child(3) > td:nth-child(5) > a]")  # 祝福贺卡-查看
    card_message_loc = ("name", "card_message")  # 祝福贺卡-祝福语输入框
    selected_red_envelopes_loc = ("id", "ECS_BONUS")  # 已有红包选择框
    input_red_number_loc = ("class name", "inputBg")  # 输入红包序列号
    validate_bonus_loc = ("name", "validate_bonus")  # 红包验证按钮
    need_inv_loc = ("name", "need_inv")  # 需要发票单选框
    inv_type_loc = ("name", "inv_type")  # 发票类型选择下拉框
    inv_payee_loc = ("name", "inv_payee")  # 发票抬头输入框
    postscript_loc = ("name", "postscript")  # 订单附言输入框
    wait_all_goods_loc = ("css selector", "input[name=how_oos][value='0']")  # 缺货处理-等待商品备齐
    cancel_order_loc = ("css selector", "input[name=how_oos][value='1']")  # 缺货处理-取消订单
    consult_loc = ("css selector", "input[name=how_oos][value='2']")  # 缺货处理-店主协商
    order_submit_loc = ("css selector", "input[type='image']")  # 提交订单
    total_expenses_shipping_loc = (
        "css selector", "#ECS_ORDERTOTAL > table > tbody > tr:nth-child(2) > td > font:nth-child(2)")  # 费用总计-配送费
    total_expenses_goods_sum_price_loc = (
        "css selector", "  tr:nth-child(2) > td > font:nth-child(1)")  # 费用总计-商品总价
    total_expenses_price_loc = (
        "css selector", "#ECS_ORDERTOTAL > table > tbody > tr:nth-child(3) > td > font")  # 费用总计-应付金额
    my_order_number_loc = ("xpath", "//div[6]/div/h6/font")  # 下单生成的订单号
    my_order_money_loc = ("xpath", "//div[6]/div/table/tbody/tr[1]/td/strong[3]")  # 下单成功后显示的订单金额
    return_home_page_loc = ("link text", "返回首页")  # 返回首页链接
    go_user_center_loc = ("link text", "用户中心")  # 去用户中心链接
    my_user_order_loc = ("xpath", "//div[6]/div[1]/div/div/div/div/a[3]/img")  # 用户中心-我的订单
    my_user_order_number_loc = ('css selector', "a[class='f6']")  # 用户中心-我的订单-所有订单编号
    my_user_order_status_loc = ("css selector", "td[style='position:relative']")  # 用户中心-我的订单_所有订单状态

    def click_search_button(self):
        """点击搜索按钮"""
        self.click(locator=self.loc_search_button)

    def click_buy_foods(self):
        """点击立即购买按钮"""
        self.click(locator=self.buy_goods_loc)

    def click_shopping_cart_accounts(self):
        """点击购物车去结算"""
        self.click(self.shopping_cart_accounts_loc)

    def get_goods_list_sum_price(self):
        """获取商品列表购物金额小计"""
        str = self.get_text(locator=self.goods_list_sum_price_loc)
        print(str)
        return str[7:16:1]

    def click_alter_goods_list(self):
        """点击修改商品列表"""
        self.click(self.shopping_goods_alter_loc)

    def click_shipping_method_sto(self):
        """点击配送方式-申通"""
        self.click_radio(self.shipping_sto_loc)

    def click_shipping_method_post_office(self):
        """点击配送方式-邮政"""
        self.click_radio(self.shipping_post_office_loc)

    def get_shipping_price_sto(self):
        """获取配送价格-申通"""
        return self.get_text(self.shipping_price_sto_loc)

    def get_shipping_price_post_office(self):
        """获取配送价格-邮政"""
        return self.get_text(self.shipping_post_office_price_loc)

    def click_pay_method_balance(self):
        """点击付款方式-余额"""
        self.click_radio(self.payment_balance_loc)

    def click_pay_method_bank(self):
        """点击付款方式-银行转账"""
        self.click_radio(self.payment_bank_transfer_loc)

    def click_unwanted_packing(self):
        """包装方式-不需要包装"""
        self.click_radio(self.packing_unwanted_packing_loc)

    def click_delicate_packing(self):
        """包装方式-精品包装"""
        self.click_radio(self.packing_delicate_packing_loc)

    def click_unwanted_card(self):
        """祝福贺卡-不需要祝福贺卡"""
        self.click_radio(self.card_unwanted_card_loc)

    def click_blessing_card(self):
        """祝福贺卡-需要祝福贺卡"""
        self.click_radio(self.card_blessing_card_loc)

    def input_card_message(self, text):
        """祝福贺卡祝福语输入"""
        self.send_keys(text=text, locator=self.card_message_loc)

    def click_need_inv(self):
        """发票选择框"""
        self.click(self.need_inv_loc)

    def choice_inv_type(self, index):
        """发票类型选择"""
        self.select_drop_down_menu(index=index, locator=self.inv_type_loc)

    def input_inv_payee(self, text):
        """发票抬头输入"""
        self.send_keys(text=text, locator=self.inv_payee_loc)

    def input_postscript(self, text):
        """订单附言输入"""
        self.send_keys(text=text, locator=self.postscript_loc)

    def click_wait_all_goods(self):
        """缺货处理-等待补货"""
        self.click_radio(self.wait_all_goods_loc)

    def click_cancel_order(self):
        """缺货处理-取消订单"""
        self.click_radio(self.cancel_order_loc)

    def click_consult(self):
        """缺货处理-与店家商议"""
        self.click_radio(self.consult_loc)

    def click_order_submit(self):
        """提交订单"""
        self.click(self.order_submit_loc)
        # time.sleep(2)

    def click_all_goods(self, index):
        """点击搜索页面商品"""
        goods = self.find_elements(self.goods_list)  # 定位所有商品元素
        goods_list = [i.get_attribute('alt') for i in goods]
        for i in goods_list:
            if i == goods_list[index]:  # 如果i=列表指定索引的数据
                goods_loc = ('css selector', f'img[alt="{i}"]')  # 针对单个商品编写对应的定位器
                self.click(locator=goods_loc)  # 点击该商品
                # time.sleep(2)
                break

    def get_total_expenses_shipping(self):
        """获取费用总计的配送价格"""
        return self.get_text(self.total_expenses_shipping_loc)

    def get_total_expenses_goods_sum_price(self):
        """获取费用总计的商品总价"""
        return self.get_text(self.total_expenses_goods_sum_price_loc)

    def get_total_expenses_price(self):
        """获取费用总计应付金额"""
        return self.get_text(self.total_expenses_price_loc)

    def get_order_submit_number(self):
        """获取下单生成的订单号"""
        return self.get_text(locator=self.my_order_number_loc)

    def get_order_submit_money(self):
        """获取下单生成的金额"""
        return self.get_text(locator=self.my_order_money_loc)

    def click_return_home_page(self):
        """点击首页"""
        self.click(locator=self.return_home_page_loc)

    def click_user_center(self):
        """点击用户中心"""
        self.click(locator=self.go_user_center_loc)

    def click_user_order(self):
        """点击我的订单"""
        self.click(locator=self.my_user_order_loc)

    def get_order_number(self, index):
        """获取订单号列表"""  # 索引从0开始
        element = self.find_elements(locator=self.my_user_order_number_loc)
        number = [i.text for i in element]
        for i in number:
            if i == number[index]:
                print(i)
                return i

    def get_order_status(self, index):
        """获取订单状态列表"""
        orders_status = ("css selector", f" tbody > tr:nth-child({index}) > td:nth-child(4)")  # 索引从0开始
        return self.get_text(orders_status)

    def cancel_order_user_center(self, index):
        """用户中心-我的订单-取消订单"""
        cancel_orders = ("css selector", f" tr:nth-child({index}) > td:nth-child(5) > font > a")  # 取消订单
        self.click(cancel_orders)

    def find_cancel_order_alert_accept(self):
        """捕获取消订单弹窗-并点击确定"""
        self.get_alert_accept_text()

    def find_cancel_order_alert_dismiss(self):
        """捕获取消订单弹窗-并点击取消"""
        self.get_alert_dismiss()


if __name__ == '__main__':
    import time

    driver = open_browser()  # 打开浏览器
    submit = OrderPage(driver)  # 实例化对象
    submit.open_url(url)  # 打开网址
    submit.skip_login()  # 跳过登录
    submit.click_search_button()  # 点击搜索按钮
    submit.click_all_goods(2)  # 选择商品
    submit.click_buy_foods()  # 点击立即购买
    time.sleep(5)
    submit.click_search_button()  # 点击搜索按钮
    time.sleep(2)
    submit.click_all_goods(3)  # 选择商品
    time.sleep(2)
    submit.click_buy_foods()  # 点击立即购买
    time.sleep(2)
    submit.click_shopping_cart_accounts()  # 点击购物车的去结算
    time.sleep(2)
    submit.get_goods_list_sum_price()
    # submit.click_shipping_method_sto()  # 点击配送方式-申通
    # submit.click_pay_method_balance()  # 点击支付方式
    # submit.click_wait_all_goods()  # 缺货处理方式-等待

    # submit.click_order_submit()  # 提交订单
    # submit.get_order_submit_number()  # 获取订单生成的订单号
    # submit.get_order_submit_money()  # 获取订单提交生成的金额
    # submit.click_user_center()  # 点击进入用户中心
    # submit.click_user_order()  # 点击用户中心-我的订单
    # submit.get_order_number(1)  # 获取指定索引的订单号
    # submit.get_order_status(1)  # 获取指定索引的订单状态

    # submit.close_browser()
